package test.my.com.myapplicationtest.cover_guide_view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import test.my.com.myapplicationtest.R;
import test.my.com.myapplicationtest.utils.DensityUtil;

/**
 * Author : cooper
 * Time :  2018/4/16 上午11:17
 * Description :
 */

public class CoverGuideView extends FrameLayout {
    private int mWidth = 100;
    private int mHeight = 100;
    private Context mContext;
    private RelativeLayout mGuideRl;

    private int mGuideWidth;
    private int mGuideHeight;
    private int mSpace = 10; // 间隔

    public CoverGuideView(@NonNull Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    public CoverGuideView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    public CoverGuideView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
//        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.YuanHuArcView,defStyleAttr,0);
//        mArcColor = ta.getColor(R.styleable.YuanHuArcView_arcColor, Color.RED);
//        mArcStrokeWidth = ta.getDimensionPixelOffset(R.styleable.YuanHuArcView_arcStrokeWidth,(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2,
//                getResources().getDisplayMetrics()));
//        mStartAngle = ta.getInt(R.styleable.YuanHuArcView_startAngle,0);
//        mSweepAngle = ta.getInt(R.styleable.YuanHuArcView_sweepAngle,100);
//        mAnimatorTime = ta.getInt(R.styleable.YuanHuArcView_animatorTime,1000);
//        ta.recycle();

        init();
    }

    private void init() {
        this.setBackgroundColor(Color.parseColor("#3319233C"));
        mGuideWidth = DensityUtil.dip2px(mContext,220);
        mGuideHeight = DensityUtil.dip2px(mContext,160);
        createGuideView();

    }

    private void createGuideView() {
        mGuideRl = new RelativeLayout(mContext);
        mGuideRl.setBackgroundColor(Color.BLUE);
        TextView tvContent = new TextView(mContext);
        tvContent.setText("5方面诊断城市能力");
        tvContent.setTextSize(TypedValue.COMPLEX_UNIT_SP,16);
        tvContent.setTextColor(Color.parseColor("#78849A"));
        LinearLayout.LayoutParams tvContentLp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        TextView tvIKonw = new TextView(mContext);
        tvIKonw.setText("我知道了");
        tvIKonw.setTextSize(TypedValue.COMPLEX_UNIT_SP,14);
        tvIKonw.setTextColor(Color.parseColor("#3778F2"));
        LinearLayout.LayoutParams tvIKonwLp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        tvIKonwLp.topMargin = DensityUtil.dip2px(mContext,20);

        LinearLayout linearLayout = new LinearLayout(mContext);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setGravity(Gravity.CENTER);

        linearLayout.addView(tvContent,tvContentLp);
        linearLayout.addView(tvIKonw,tvIKonwLp);

        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);

        mGuideRl.addView(linearLayout,layoutParams);
        mGuideRl.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (CoverGuideView.this.getChildCount() > 0) {
                    CoverGuideView.this.removeAllViews();
                }
                requestLayout();
            }
        });

    }

    public void showTopGuide(View targetView, int x, int y) {
        int width = targetView.getWidth();
        int height = targetView.getHeight();
        int i = x+width/2;
        int j = y-mSpace;
        int ml = i - mGuideWidth/2;
        int mt = j - mGuideHeight -63;
        mGuideRl.setBackgroundResource(R.drawable.guide_bottom);
        showGuide(ml,mt);
    }

    public void showBottomGuide(View targetView, int x, int y) {
        int width = targetView.getWidth();
        int height = targetView.getHeight();
        int i = x+width/2;
        int j = y+height;
        int ml = i - mGuideWidth/2;
        int mt = j +mSpace -63;
        mGuideRl.setBackgroundResource(R.drawable.guide_top);
        showGuide(ml,mt);
    }

    public void showLeftGuide(View targetView, int x, int y) {
        int width = targetView.getWidth();
        int height = targetView.getHeight();
        int i = x-mSpace;
        int j = y+height/2;
        int ml = i - mGuideWidth;
        int mt = j-mGuideHeight/2 - 63;
        mGuideRl.setBackgroundResource(R.drawable.guide_right);
        showGuide(ml,mt);
    }

    public void showRightGuide(View targetView, int x, int y) {
        int width = targetView.getWidth();
        int height = targetView.getHeight();
        int i = x+width+mSpace;
        int j = y+height/2;
        int ml = i ;
        int mt = j-mGuideHeight/2 - 63;
        mGuideRl.setBackgroundResource(R.drawable.guide_left);
        showGuide(ml,mt);
    }

    private void showGuide(int ml, int mt) {
        if (this.getChildCount() > 0) {
            this.removeAllViews();
        }
        FrameLayout.LayoutParams layoutParams = new LayoutParams(DensityUtil.dip2px(mContext,220),DensityUtil.dip2px(mContext,160));
        layoutParams.setMargins(ml,mt,0,0);
        this.addView(mGuideRl,layoutParams);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(measureWidth(widthMeasureSpec),measureHeight(heightMeasureSpec));
    }


    private int measureWidth(int widthMeasureSpec){
        int result = 0;
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);
        if(specMode == MeasureSpec.EXACTLY){
            result = specSize;
        } else {
            result = mWidth;
            if(specMode == MeasureSpec.AT_MOST){
                result = Math.min(result, specSize);
            }
        }
        this.mWidth = result;
        return result;
    }

    private int measureHeight(int heightMeasureSpec){
        int result = 0;
        int specMode = MeasureSpec.getMode(heightMeasureSpec);
        int specSize = MeasureSpec.getSize(heightMeasureSpec);
        if(specMode == MeasureSpec.EXACTLY){
            result = specSize;
        } else {
            result = mHeight;
            if(specMode == MeasureSpec.AT_MOST){
                result = Math.min(result, specSize);
            }
        }
        this.mHeight = result;
        return result;
    }
}
